home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime vr / vrcursors / application files / comapplication.c next >
Encoding:
Text File  |  2000-09-28  |  15.9 KB  |  576 lines

  1. //////////
  2. //
  3. //    File:        ComApplication.c
  4. //
  5. //    Contains:    Application-specific code for cursor changing demo.
  6. //                This file is used for BOTH MacOS and Windows.
  7. //
  8. //    Written by:    Tim Monroe
  9. //                Based (heavily!) on the MovieShell code written by Apple DTS.
  10. //
  11. //    Copyright:    © 1994-1997 by Apple Computer, Inc., all rights reserved.
  12. //
  13. //    Change History (most recent first):
  14. //
  15. //       <11>         10/23/97    rtm        moved InitializeQTVR to InitApplication, TerminateQTVR to StopApplication
  16. //       <10>         10/13/97    rtm        reworked HandleApplicationMenu to use menu identifiers
  17. //       <9>         09/11/97    rtm        merged MacApplication.c and WinApplication.c into ComApplication.c
  18. //       <8>         08/21/97    rtm        first file for Windows; based on MacApplication.c for Mac sample code
  19. //       <7>         06/27/97    rtm        added code to change some hot spot cursors
  20. //       <6>         06/04/97    rtm        removed call to QTVRUtils_IsQTVRMovie in InitApplicationWindowObject
  21. //       <5>         02/06/97    rtm        fixed window resizing code
  22. //       <4>         12/05/96    rtm        added hooks into MacFramework.c: StopApplication, InitApplicationWindowObject
  23. //       <3>         12/02/96    rtm        added cursor updating to DoIdle
  24. //       <2>         11/27/96    rtm        conversion to personal coding style; added preliminary QTVR support
  25. //       <1>         12/21/94    khs        first file
  26. //       
  27. //////////
  28.  
  29. // header files
  30. #include "ComApplication.h"
  31.  
  32. // header file for the specific test functions.
  33. #include "TestFunctions.h"
  34.  
  35.  
  36. // global variables for Macintosh code
  37. #if TARGET_OS_MAC
  38. #endif
  39.  
  40. // global variables for Windows code
  41. #if TARGET_OS_WIN32
  42. extern HWND                ghWnd;                                    // the MDI frame window; this window has the menu bar
  43. extern int                gNumWindowsOpen;
  44. extern LPSTR            gCmdLine;
  45. #endif
  46.  
  47. long                    gMaxMilliSecToUse = 0L;        
  48. Boolean                    gQTVRMgrIsPresent = false;                // is the QuickTime VR Manager available?        
  49. long                    gQTVRMgrVersion = 0L;                    // the version of the QuickTime VR Manager    
  50.  
  51.  
  52. //////////
  53. //
  54. // InitApplication
  55. // Do any application-specific initialization.
  56. //
  57. // The theStartPhase parameter determines which "phase" of application start-up is executed,
  58. // •before• the MDI frame window is created or •after•. This distinction is relevant only on
  59. // Windows, so on MacOS, you should always use kInitAppPhase_BothPhases.
  60. //
  61. //////////
  62.  
  63. void InitApplication (UInt32 theStartPhase)
  64. {
  65.     // •••do any start-up activities that should occur before the MDI frame window is created
  66.     if (theStartPhase & kInitAppPhase_BeforeCreateFrameWindow) {
  67.  
  68.         // make sure that the QuickTime VR Manager is available in the present operating environment;
  69.         // if it is, get its version and (if necessary) initialize it
  70.         if (QTVRUtils_IsQTVRMgrInstalled()) {
  71.             gQTVRMgrIsPresent = true;
  72.             gQTVRMgrVersion = QTVRUtils_GetQTVRVersion();        // get the version of the QuickTime VR Manager
  73.         
  74. #if TARGET_OS_WIN32
  75.             // initialize the QuickTime VR Manager
  76.             InitializeQTVR();                                    
  77. #endif
  78.         }
  79.         
  80.     }    // end of kInitAppPhase_BeforeCreateFrameWindow
  81.  
  82.     // •••do any start-up activities that should occur after the MDI frame window is created
  83.     if (theStartPhase & kInitAppPhase_AfterCreateFrameWindow) {
  84.         
  85. #if TARGET_OS_WIN32
  86.         // on Windows, open as movie documents any files specified on the command line
  87.         DoOpenCommandLineMovies(gCmdLine);                                    
  88. #endif
  89.  
  90.     }    // end of kInitAppPhase_AfterCreateFrameWindow
  91. }
  92.  
  93.  
  94. //////////
  95. //
  96. // StopApplication
  97. // Do any application-specific shut-down.
  98. //
  99. // The theStopPhase parameter determines which "phase" of application shut-down is executed,
  100. // •before• any open movie windows are destroyed or •after•.
  101. //
  102. //////////
  103.  
  104. void StopApplication (UInt32 theStopPhase)
  105. {
  106.     // do any shut-down activities that should occur after the movie windows are destroyed
  107.     if (theStopPhase & kStopAppPhase_AfterDestroyWindows) {
  108.     
  109. #if TARGET_OS_WIN32
  110.         // terminate QuickTime VR Manager
  111.         if (gQTVRMgrIsPresent)
  112.             TerminateQTVR();
  113. #endif
  114.             
  115.     }    // end of kStopAppPhase_AfterDestroyWindows
  116. }
  117.  
  118.  
  119. //////////
  120. //
  121. // DoIdle
  122. // Do any processing that can/should occur at idle time.
  123. //
  124. //////////
  125.  
  126. void DoIdle (WindowReference theWindow)
  127. {
  128.     WindowObject         myWindowObject = NULL;
  129.     GrafPtr             mySavedPort;
  130.     
  131.     GetPort(&mySavedPort);
  132.     MacSetPort(GetPortFromWindowReference(theWindow));
  133.     
  134.     myWindowObject = GetWindowObjectFromWindow(theWindow);
  135.     if (myWindowObject != NULL) {
  136.         MovieController        myMC = NULL;
  137.     
  138.         myMC = (**myWindowObject).fController;
  139.         if (myMC != NULL) {
  140.  
  141. #if TARGET_OS_MAC
  142.             // restore the cursor to the arrow
  143.             // if it's outside the front movie window or outside the window's visible region
  144.             if (theWindow == GetFrontMovieWindow()) {
  145.                 Rect    myRect;
  146.                 Point    myPoint;
  147.                 
  148.                 GetMouse(&myPoint);
  149.                 MCGetControllerBoundsRect(myMC, &myRect);
  150.                 if (!MacPtInRect(myPoint, &myRect) || !PtInRgn(myPoint, GetPortFromWindowReference(theWindow)->visRgn))
  151.                     MacSetCursor(&qd.arrow);
  152.             }
  153. #endif    // TARGET_OS_MAC
  154.         }
  155.     }
  156.  
  157.     // @@@INSERT APPLICATION-SPECIFIC IDLE-TIME FUNCTIONALITY HERE
  158.  
  159.     MacSetPort(mySavedPort);
  160. }
  161.  
  162.  
  163. //////////
  164. //
  165. // DoUpdateWindow
  166. // Update the specified window.
  167. //
  168. //////////
  169.  
  170. void DoUpdateWindow (WindowReference theWindow, Rect *theRefreshArea)
  171. {
  172.     GrafPtr             mySavedPort;
  173.     
  174.     GetPort(&mySavedPort);
  175.     MacSetPort(GetPortFromWindowReference(theWindow));
  176.     
  177.     BeginUpdate(GetPortFromWindowReference(theWindow));
  178.     
  179.     // draw the movie controller and its movie
  180.     MCDoAction(GetMCFromWindow(theWindow), mcActionDraw, theWindow);
  181.     
  182.     EndUpdate(GetPortFromWindowReference(theWindow));
  183.     MacSetPort(mySavedPort);
  184. }
  185.  
  186.  
  187. //////////
  188. //
  189. // HandleContentClick
  190. // Handle mouse button clicks in the specified window.
  191. //
  192. //////////
  193.  
  194. void HandleContentClick (WindowReference theWindow, EventRecord *theEvent)
  195. {
  196.     GrafPtr             mySavedPort;
  197.     
  198.     GetPort(&mySavedPort);
  199.     MacSetPort(GetPortFromWindowReference(theWindow));
  200.     
  201.     // @@@INSERT APPLICATION-SPECIFIC CONTENT CLICKING FUNCTIONALITY HERE
  202.  
  203.     MacSetPort(mySavedPort);
  204. }
  205.  
  206.  
  207. //////////
  208. //
  209. // HandleApplicationKeyPress
  210. // Handle application-specific key presses.
  211. // Returns true if the key press was handled, false otherwise.
  212. //
  213. //////////
  214.  
  215. Boolean HandleApplicationKeyPress (char theCharCode)
  216. {
  217.     Boolean        isHandled = true;
  218.     
  219.     switch (theCharCode) {
  220.     
  221.         // @@@HANDLE APPLICATION-SPECIFIC KEY PRESSES HERE
  222.  
  223.         default:
  224.             isHandled = false;
  225.             break;
  226.     }
  227.  
  228.     return(isHandled);
  229. }
  230.  
  231.  
  232. #if TARGET_OS_MAC
  233. //////////
  234. //
  235. // CreateMovieWindow
  236. // Create a window to display a movie in.
  237. //
  238. //////////
  239.  
  240. WindowRef CreateMovieWindow (Rect *theRect, Str255 theTitle)
  241. {
  242.     WindowRef            myWindow;
  243.         
  244.     myWindow = NewCWindow(NULL, theRect, theTitle, false, noGrowDocProc, (WindowPtr)-1L, true, 0);
  245.     return(myWindow);
  246. }
  247. #endif
  248.  
  249.  
  250. //////////
  251. //
  252. // HandleApplicationMenu
  253. // Handle selections in the application's menus.
  254. //
  255. // The theMenuItem parameter is a UInt16 version of the Windows "menu item identifier". 
  256. // When called from Windows, theMenuItem is simply the menu item identifier passed to the window proc.
  257. // When called from MacOS, theMenuItem is constructed like this:
  258. //     •high-order 8 bits == the Macintosh menu ID (1 thru 256)
  259. //     •low-order 8 bits == the Macintosh menu item (sequential from 1 to ordinal of last menu item in menu)
  260. // In this way, we can simplify the menu-handling code. There are, however, some limitations,
  261. // mainly that the menu item identifiers on Windows must be derived from the Mac values. 
  262. //
  263. //////////
  264.  
  265. void HandleApplicationMenu (UInt16 theMenuItem)
  266. {
  267.     WindowObject        myWindowObject = NULL;
  268.     MovieController     myMC = NULL;
  269.     
  270.     myWindowObject = GetWindowObjectFromFrontWindow();
  271.     if (myWindowObject != NULL)
  272.         myMC = (**myWindowObject).fController;
  273.     
  274.     // make sure we have a valid movie controller
  275.     if (myMC == NULL)
  276.         return;
  277.     
  278.     switch (theMenuItem) {
  279.         case IDM_CONTROLLER:
  280.             QTUtils_ToggleControllerBar(myMC);
  281.             break;
  282.  
  283.         case IDM_SPEAKER_BUTTON:
  284.             if (QTUtils_IsControllerButtonVisible(myMC, kQTVRSpeakerButton))
  285.                 QTUtils_HideControllerButton(myMC, kQTVRSpeakerButton);
  286.             else 
  287.                 QTUtils_ShowControllerButton(myMC, kQTVRSpeakerButton);
  288.             break;
  289.  
  290.         case IDM_BACK_BUTTON:
  291.             if (QTUtils_IsControllerButtonVisible(myMC, kQTVRBackButton))
  292.                 QTUtils_HideControllerButton(myMC, kQTVRBackButton);
  293.             else 
  294.                 QTUtils_ShowControllerButton(myMC, kQTVRBackButton);
  295.             break;
  296.  
  297.         case IDM_ZOOM_BUTTONS:
  298.             if (QTUtils_IsControllerButtonVisible(myMC, kQTVRZoomButtons))
  299.                 QTUtils_HideControllerButton(myMC, kQTVRZoomButtons);
  300.             else 
  301.                 QTUtils_ShowControllerButton(myMC, kQTVRZoomButtons);
  302.             break;
  303.  
  304.         case IDM_HOTSPOT_BUTTON:
  305.             if (QTUtils_IsControllerButtonVisible(myMC, kQTVRHotSpotButton))
  306.                 QTUtils_HideControllerButton(myMC, kQTVRHotSpotButton);
  307.             else 
  308.                 QTUtils_ShowControllerButton(myMC, kQTVRHotSpotButton);
  309.             break;
  310.  
  311.         case IDM_TRANSLATE_BUTTON:
  312.             if (QTUtils_IsControllerButtonVisible(myMC, kQTVRTranslateButton))
  313.                 QTUtils_HideControllerButton(myMC, kQTVRTranslateButton);
  314.             else 
  315.                 QTUtils_ShowControllerButton(myMC, kQTVRTranslateButton);
  316.             break;
  317.         
  318.         default:
  319.             break;
  320.     }    // switch (theMenuItem)
  321. }
  322.  
  323.  
  324. //////////
  325. //
  326. // AdjustApplicationMenus
  327. // Adjust state of items in the application's menus.
  328. //
  329. // Currently, the Mac application has only one app-specific menu ("Test"); you could change that.
  330. //
  331. //////////
  332.  
  333. void AdjustApplicationMenus (WindowReference theWindow, MenuReference theMenu)
  334. {
  335.     WindowObject        myWindowObject = NULL; 
  336.     MovieController     myMC = NULL;
  337.     MenuReference        myMenu;
  338.     
  339. #if TARGET_OS_WIN32
  340.     myMenu = theMenu;
  341. #elif TARGET_OS_MAC
  342.     myMenu = GetMenuHandle(kTestMenu);
  343. #endif
  344.     
  345.     if (theWindow != NULL)
  346.         myWindowObject = GetWindowObjectFromWindow(theWindow);
  347.  
  348.     if (myWindowObject != NULL)
  349.         myMC = (**myWindowObject).fController;
  350.  
  351.     // we don't allow creating new files here...
  352. #if TARGET_OS_MAC
  353.     SetMenuItemState(GetMenuHandle(mFile), iNew, kDisableMenuItem);
  354. #endif
  355.  
  356.     if (myMC == NULL) {
  357.         // no movie controller, so disable all the Test menu items
  358.         SetMenuItemState(myMenu, IDM_CONTROLLER, kDisableMenuItem);
  359.         SetMenuItemState(myMenu, IDM_SPEAKER_BUTTON, kDisableMenuItem);
  360.         SetMenuItemState(myMenu, IDM_BACK_BUTTON, kDisableMenuItem);
  361.         SetMenuItemState(myMenu, IDM_ZOOM_BUTTONS, kDisableMenuItem);
  362.         SetMenuItemState(myMenu, IDM_HOTSPOT_BUTTON, kDisableMenuItem);
  363.         SetMenuItemState(myMenu, IDM_TRANSLATE_BUTTON, kDisableMenuItem);
  364.     } else {
  365.         // we've got a movie controller, so it's safe to proceed....
  366.         SetMenuItemState(myMenu, IDM_CONTROLLER, kEnableMenuItem);
  367.  
  368.         // if controller bar is visible, enable Test menu items
  369.         if (QTUtils_IsControllerBarVisible(myMC)) {
  370.             SetMenuItemLabel(myMenu, IDM_CONTROLLER, "Hide &Controller");
  371.                // ungray the other menu itmes
  372.             SetMenuItemState(myMenu, IDM_SPEAKER_BUTTON, kEnableMenuItem);
  373.             SetMenuItemState(myMenu, IDM_BACK_BUTTON, kEnableMenuItem);
  374.             SetMenuItemState(myMenu, IDM_ZOOM_BUTTONS, kEnableMenuItem);
  375.             SetMenuItemState(myMenu, IDM_HOTSPOT_BUTTON, kEnableMenuItem);
  376.             SetMenuItemState(myMenu, IDM_TRANSLATE_BUTTON, kEnableMenuItem);
  377.             
  378.             // handle QT and QTVR Test menu items
  379.             if (QTUtils_IsControllerButtonVisible(myMC, kQTVRSpeakerButton))
  380.                 SetMenuItemLabel(myMenu, IDM_SPEAKER_BUTTON, "Hide Spea&ker Button");
  381.             else 
  382.                 SetMenuItemLabel(myMenu, IDM_SPEAKER_BUTTON, "Show Spea&ker Button");
  383.  
  384.             // handle QTVR-specific Test menu items
  385.             if ((**myWindowObject).fInstance != NULL) {
  386.  
  387.                 if (QTUtils_IsControllerButtonVisible(myMC, kQTVRBackButton))
  388.                     SetMenuItemLabel(myMenu, IDM_BACK_BUTTON, "Hide &Back Button");
  389.                 else 
  390.                     SetMenuItemLabel(myMenu, IDM_BACK_BUTTON, "Show &Back Button");
  391.  
  392.                 if (QTUtils_IsControllerButtonVisible(myMC, kQTVRZoomButtons))
  393.                     SetMenuItemLabel(myMenu, IDM_ZOOM_BUTTONS, "Hide &Zoom Buttons");
  394.                 else 
  395.                     SetMenuItemLabel(myMenu, IDM_ZOOM_BUTTONS, "Show &Zoom Buttons");
  396.  
  397.                 if (QTUtils_IsControllerButtonVisible(myMC, kQTVRHotSpotButton))
  398.                     SetMenuItemLabel(myMenu, IDM_HOTSPOT_BUTTON, "Hide &Hot Spot Button");
  399.                 else 
  400.                     SetMenuItemLabel(myMenu, IDM_HOTSPOT_BUTTON, "Show &Hot Spot Button");
  401.  
  402.                 if (QTUtils_IsControllerButtonVisible(myMC, kQTVRTranslateButton))
  403.                     SetMenuItemLabel(myMenu, IDM_TRANSLATE_BUTTON, "Hide &Translate Button");
  404.                 else 
  405.                     SetMenuItemLabel(myMenu, IDM_TRANSLATE_BUTTON, "Show &Translate Button");
  406.  
  407.             } else {    // we're not a QTVR movie, so disable QTVR-specific menus
  408.                 SetMenuItemState(myMenu, IDM_BACK_BUTTON, kDisableMenuItem);
  409.                 SetMenuItemState(myMenu, IDM_ZOOM_BUTTONS, kDisableMenuItem);
  410.                 SetMenuItemState(myMenu, IDM_HOTSPOT_BUTTON, kDisableMenuItem);
  411.                 SetMenuItemState(myMenu, IDM_TRANSLATE_BUTTON, kDisableMenuItem);
  412.             }
  413.  
  414.             
  415.         } else {
  416.             // controller bar is not visible
  417.             
  418.             SetMenuItemLabel(myMenu, IDM_CONTROLLER, "Show &Controller");
  419.               // gray the other menu items
  420.             SetMenuItemState(myMenu, IDM_SPEAKER_BUTTON, kDisableMenuItem);
  421.             SetMenuItemState(myMenu, IDM_BACK_BUTTON, kDisableMenuItem);
  422.             SetMenuItemState(myMenu, IDM_ZOOM_BUTTONS, kDisableMenuItem);
  423.             SetMenuItemState(myMenu, IDM_HOTSPOT_BUTTON, kDisableMenuItem);
  424.             SetMenuItemState(myMenu, IDM_TRANSLATE_BUTTON, kDisableMenuItem);
  425.         }
  426.     }
  427. }
  428.  
  429.  
  430. //////////
  431. //
  432. // DoApplicationEventLoopAction
  433. // Perform any application-specific event loop actions.
  434. //
  435. // Return true to indicate that we've completely handled the event here, false otherwise.
  436. //
  437. //////////
  438.  
  439. Boolean DoApplicationEventLoopAction (EventRecord *theEvent)
  440. {
  441.     return(false);            // no-op for now
  442. }
  443.  
  444.  
  445. //////////
  446. //
  447. // AddControllerFunctionality
  448. // Configure the movie controller.
  449. //
  450. //////////
  451.  
  452. void AddControllerFunctionality (MovieController theMC)
  453. {
  454.     long            myControllerFlags;
  455.     
  456.     // CLUT table use    
  457.     MCDoAction(theMC, mcActionGetFlags, &myControllerFlags);
  458.     MCDoAction(theMC, mcActionSetFlags, (void *)(myControllerFlags | mcFlagsUseWindowPalette));
  459.  
  460.     // enable keyboard event handling    
  461.     MCDoAction(theMC, mcActionSetKeysEnabled, (void *)true);
  462.     
  463.     // disable drag support
  464.     MCDoAction(theMC, mcActionSetDragEnabled, (void *)false);
  465. }
  466.  
  467.  
  468. //////////
  469. //
  470. // InitApplicationWindowObject
  471. // Do any application-specific initialization of the window object.
  472. //
  473. //////////
  474.  
  475. void InitApplicationWindowObject (WindowObject theWindowObject)
  476. {
  477.     Track                        myQTVRTrack = NULL;
  478.     Movie                        myMovie = NULL;
  479.     MovieController                myMC = NULL;
  480.     QTVRInstance                myInstance = NULL;
  481.     QTVRMouseOverHotSpotUPP        myInterceptProc;
  482.         
  483.     if (theWindowObject == NULL)
  484.         return;
  485.  
  486.     // make sure we can safely call the QTVR API
  487.     if (!gQTVRMgrIsPresent)
  488.         return;
  489.  
  490.     // find the QTVR track, if there is one
  491.     myMC = (**theWindowObject).fController;
  492.     myMovie = (**theWindowObject).fMovie;
  493.     myQTVRTrack = QTVRGetQTVRTrack(myMovie, 1);
  494.     
  495.     QTVRGetQTVRInstance(&myInstance, myQTVRTrack, myMC);
  496.     (**theWindowObject).fInstance = myInstance;
  497.  
  498.     // do any QTVR window configuration
  499.     if (myInstance != NULL) {
  500.         
  501.         // set unit to radians
  502.         QTVRSetAngularUnits(myInstance, kQTVRRadians);
  503.  
  504.         // update
  505.         QTVRUpdate(myInstance, kQTVRCurrentMode);
  506.         
  507.         // install a mouse-over hot spot procedure to change cursors for specific hot spot types
  508.         myInterceptProc = NewQTVRMouseOverHotSpotProc(MyMouseOverHotSpotProc);
  509.         QTVRSetMouseOverHotSpotProc(myInstance, myInterceptProc, 0, 0);
  510.     }
  511. }
  512.  
  513.  
  514. //////////
  515. //
  516. // RemoveApplicationWindowObject
  517. // Do any application-specific clean-up of the window object.
  518. //
  519. //////////
  520.  
  521. void RemoveApplicationWindowObject (WindowObject theWindowObject)
  522. {
  523.     OSErr                myErr = noErr;
  524.     QTVRInstance        myInstance = NULL;
  525.         
  526.     if (theWindowObject == NULL)
  527.         return;
  528.         
  529.     myInstance = (**theWindowObject).fInstance;
  530.     
  531.     // @@@INSERT APPLICATION-SPECIFIC WINDOW OBJECT CLEAN-UP HERE
  532.  
  533.     // DoDestroyMovieWindow in MacFramework.c or MovieWndProc in WinFramework.c
  534.     // releases the window object itself
  535. }
  536.  
  537.  
  538. //////////
  539. //
  540. // ApplicationMCActionFilterProc 
  541. // Intercept some mc actions for the movie controller.
  542. //
  543. // NOTE: The theRefCon parameter is a handle to a window object record.
  544. //
  545. //////////
  546.  
  547. PASCAL_RTN Boolean ApplicationMCActionFilterProc (MovieController theMC, short theAction, void *theParams, long theRefCon)
  548. {
  549. #pragma unused(theMC, theParams)
  550.  
  551.     Boolean                isHandled = false;
  552.     WindowObject        myWindowObject = NULL;
  553.     
  554.     myWindowObject = (WindowObject)theRefCon;
  555.     if (myWindowObject == NULL)
  556.         return(isHandled);
  557.         
  558.     switch (theAction) {
  559.     
  560.         // handle window resizing
  561.         case mcActionControllerSizeChanged:
  562.             SizeWindowToMovie(myWindowObject);
  563.             break;
  564.  
  565.         // handle idle events
  566.         case mcActionIdle:
  567.             DoIdle((**myWindowObject).fWindow);
  568.             break;
  569.             
  570.         default:
  571.             break;
  572.             
  573.     }    // switch (theAction)
  574.     
  575.     return(isHandled);    
  576. }